/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.board;

import java.util.*;

import java.io.File;
import javax.servlet.*;
import javax.servlet.http.*;

import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.zll.framework.util.StringUtil;




/**
 *
 * @author eastar
 */
public class CommentSaveCommand implements Command
{
    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
    	HttpSession session = (HttpSession)request.getSession();
    	String userId 		= StringUtil.nvl(session.getAttribute("session.user_id"));
    	String userName 	= StringUtil.nvl(session.getAttribute("session.user_name"));
    	String userAuth 	= StringUtil.nvl(session.getAttribute("session.user_auth"));

    	if ("".equals(userId)) {
    		throw new Exception("로그인 후 작성하세요.");
    	}

    	HashBox box = new HashBox(request);

		BoardDAO boardDAO = new BoardDAO();


		if ("C".equals(box.get("job_type"))) {
			box.put("author_name", userName);
			box.put("ip_addr",	request.getRemoteAddr());
		}
		else if ("D".equals(box.get("job_type"))) {
			if (userAuth.equals("S") || userAuth.equals("A")) {
				//관리자 삭제가능
			} else {
				HashList commentList = boardDAO.selectCommentInfo(box.getParam());
				if (!userId.equals(commentList.get("init_user",0))) {
					throw new Exception("본인만 삭제할 수 있습니다.");
				}
			}
		}


		boardDAO.saveComment(box.getParam());


		//결과 페이지 이동
		HashMap result		= new HashMap();
		result.put("result", 	"true");
		result.put("action",	"/board/wordView.jsp?word_id="+box.get("word_id"));

    	request.setAttribute("result", result);

    	return "/common/move_view.jsp";
    }
}
